//
//  WelcomeViewController.swift
//  WEIBO_SWIFT
//
//  Created by 岳挺 on 15/6/29.
//  Copyright © 2015年 yueting. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    var constrain : NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if let imageStr = sharedUserAccount?.icon {
            iconView.sd_setImageWithURL(NSURL(string: imageStr))
        }

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        constrain?.constant = -180

        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            self.iconView.layoutIfNeeded()
            }) { (_)in
                UIView.animateWithDuration(1.0, animations: { () -> Void in
                    self.label.alpha = 1.0
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(notification, object: true)
                })
        }
    }
    
    override func loadView() {
        view = UIView()
        view.addSubview(bgImageView)
        view.addSubview(iconView)
        view.addSubview(label)
        
        bgImageView.equalToSuperViewSize()
        constrain = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 50)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : iconView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : iconView]))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        view.addConstraint(constrain!)
        
        label.locateInSuperViewCenter(CGSizeMake(80, 40), centerOffSet: CGPointMake(0, -100))
    }
    
    lazy var bgImageView : UIImageView = {
        let bg = UIImageView(image: UIImage(named: "ad_background"))
        return bg
        }()
    
    lazy var iconView : UIImageView = {
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.cornerRadius = 50
        icon.layer.masksToBounds = true
        return icon
        }()
    
    lazy var label : UILabel = {
        let l = UILabel()
        l.text = "欢迎回来"
        l.textColor = UIColor.orangeColor()
        l.textAlignment = NSTextAlignment.Center
        l.font = UIFont.systemFontOfSize(17.0)
        l.alpha = 0.0
        return l
        }()
}
